<template>
  <div class="recommend" v-loading:[loadingText]="loading">
    <Scroll class="recommend-content">
      <div>
        <div class="slider-wrapper">
          <div class="slider-content">
            <slider v-if="sliderList.length" :sliders="sliderList" />
          </div>
        </div>
        <div class="recommend-list">
          <h1 class="list-title" v-show="!loading">热门歌单推荐</h1>
          <ul>
            <li
              @click="selectItem(item)"
              v-for="(item, index) in albums"
              :key="index"
              class="item"
            >
              <div class="icon">
                <img width="60" height="60" v-lazy="item.pic" alt="" />
              </div>
              <div class="text">
                <h2 class="name">{{ item.username }}</h2>
                <p class="title">{{ item.title }}</p>
              </div>
            </li>
          </ul>
        </div>
      </div>
    </Scroll>
    <router-view v-slot="{ Component }">
      <transition appear name="slide">
        <component :is="Component" :data="selectedAlbum" />
      </transition>
    </router-view>
  </div>
</template>

<script>
import { getRecommend } from '@/service/recommend';
import slider from '@/components/base/slider/slider.vue';
// import Scroll from '@/components/base/scroll/scroll.vue';
import Scroll from '../components/wrap-scroll';
import { ALBUM_KEY } from '@/assets/js/constant.js';

export default {
  name: 'recommend',
  components: {
    slider,
    Scroll
  },
  computed: {
    loading() {
      return !this.sliderList.length && !this.albums.length;
    }
  },
  data() {
    return {
      sliderList: [],
      albums: [],
      loadingText: '正在加载',
      selectedAlbum: null
    };
  },
  async created() {
    const result = await getRecommend();
    this.sliderList = result.sliders;
    this.albums = result.albums;
  },
  methods: {
    selectItem(album) {
      this.selectedAlbum = album;
      // 本地缓存,防止刷新
      this.catchAlbum(album);
      this.$router.push({
        path: `/recommend/${album.id}`
      });
    },
    catchAlbum(album) {
      const albums = JSON.stringify(album);
      sessionStorage.setItem(ALBUM_KEY, albums);
    }
  }
};
</script>

<style lang="scss" scoped>
.recommend {
  position: fixed;
  width: 100%;
  top: 88px;
  bottom: 0;
  .recommend-content {
    height: 100%;
    overflow: hidden;
    .slider-wrapper {
      position: relative;
      width: 100%;
      height: 0;
      padding-top: 40%;
      overflow: hidden;
      .slider-content {
        position: absolute;
        left: 0;
        top: 0;
        width: 100%;
        height: 100%;
      }
    }
    .recommend-list {
      .list-title {
        height: 65px;
        line-height: 65px;
        text-align: center;
        font-size: $font-size-medium;
        color: $color-theme;
      }
      .item {
        display: flex;
        box-sizing: border-box;
        align-items: center;
        padding: 0 20px 20px 20px;
      }
      .icon {
        flex: 0 0 60px;
        width: 60px;
        padding-right: 20px;
      }
      .text {
        display: flex;
        flex-direction: column;
        justify-content: center;
        flex: 1;
        line-height: 20px;
        overflow: hidden;
        font-size: $font-size-medium;
      }
      .name {
        margin-bottom: 10px;
        color: $color-text;
      }
      .title {
        color: $color-text-d;
      }
    }
  }
}
</style>
